﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.Application.Domain;
using Happy.Security.Principal;
using Happy.Security.Domain.Users;

namespace Happy.Security.Application.Authentication
{
    public sealed class AuthenticateService : ApplicationService
    {
        public AuthenticateResult Authenticate(string username, string password)
        {
            var user = this.Service<IUserRepository>().FindByUserName(username);
            if (user == null)
            {
                return new AuthenticateResult
                {
                    IsAuthenticated = false
                };
            }

            if (!this.Service<PasswordService>().CheckPassword(user, password))
            {
                return new AuthenticateResult
                {
                    IsAuthenticated = false
                };
            }

            return new AuthenticateResult
            {
                IsAuthenticated = true,
                Principal = new HappyPrincipal(new HappyIdentity(user.UserName, "HAPPY"))
            };
        }
    }
}
